package com.hspedu.newData.dmsxl.栈;

import org.junit.Test;

import java.util.Stack;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-28 
 * @Version: 1.0
 */

public class LC150逆波兰表达式求值 {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("+")){
                int i1 = stack.pop();
                int i2 = stack.pop();
                stack.push(i2+i1);
            }else if (tokens[i].equals("-")){
                int i1 = stack.pop();
                int i2 = stack.pop();
                stack.push(i2-i1);
            }else if (tokens[i].equals("*")){
                int i1 = stack.pop();
                int i2 = stack.pop();
                stack.push(i2*i1);
            }else if (tokens[i].equals("/")){
                int i1 = stack.pop();
                int i2 = stack.pop();
                stack.push(i2/i1);
            }else {
                stack.push(Integer.valueOf(tokens[i]));
            }
        }
        return stack.pop();
    }

    @Test
    public void test() {
        // 测试代码


    }
}
